using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.Configuration;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using CMS.Core.DataInterfaces;
using CMS.Core.Domain;
using CMS.Data;
using CMS.Presenters;
using AjaxPro;

namespace CMS.Web.ListManager
{
    public partial class AppSettings : BaseUserControl, IAppSettingsView
    {
        AppSettingsPresenter presenter;
        bool isFinishUpdate = false;

        public AppSettings()
        {
            this.presenter = new AppSettingsPresenter(this);
        }
        
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            presenter.UpdateCompleteEvent += new EventHandler(HandleUpdateCompleteEvent);
            presenter.CancelUpdateEvent += new EventHandler(HandleCancelUpdateEvent);
        }

        private void HandleUpdateCompleteEvent(object sender, EventArgs e)
        {
            
        }

        private void HandleCancelUpdateEvent(object sender, EventArgs e)
        {
            
        }

        #region IAppSettingsView Members

        public String DataSource {
            set { txtDataSource.Text = value; }
            get { return txtDataSource.Text.Trim();}
        }
        public String Database
        {
            set { txtDatabase.Text = value; }
            get { return txtDatabase.Text.Trim(); }
        }
        public String UserID
        {
            set { txtUserID.Text = value; }
            get { return txtUserID.Text.Trim(); }
        }
        public String Password
        {
            set { txtPassword.Text = value; }
            get { return txtPassword.Text.Trim(); }
        }
        //public String LoginType
        //{
        //    set
        //    {
        //        ListItem item = rdoLoginType.Items.FindByValue(value);
        //        if (item != null) item.Selected = true;
        //    }
        //    get
        //    {
        //        return rdoLoginType.SelectedValue;
        //    }
        //}
        //public String LDAPServer
        //{
        //    set { txtLDAPServer.Text = value; }
        //    get { return txtLDAPServer.Text.Trim(); }
        //}
        //public String LDAPLogin
        //{
        //    set { txtLDAPLogin.Text = value; }
        //    get { return txtLDAPLogin.Text.Trim(); }
        //}
        //public String LDAPAttrib
        //{
        //    set { txtLDAPAttrib.Text = value; }
        //    get { return txtLDAPAttrib.Text.Trim(); }
        //}
        //public String LDAPAttribExt
        //{
        //    set { txtLDAPAttribExt.Text = value; }
        //    get { return txtLDAPAttribExt.Text.Trim(); }
        //}
        //public String LDAPGroup
        //{
        //    set { txtLDAPGroup.Text = value; }
        //    get { return txtLDAPGroup.Text.Trim(); }
        //}
        public String UploadPath
        {
            set { txtUploadPath.Text = value; }
            get { 
                String temp = txtUploadPath.Text.Trim();
                temp = temp.Replace("\\", "/");
                if (!temp.EndsWith("/")) temp = temp + "/";
                return temp;
            }
        }
        public String MaDiaBan
        { 
            set {
                ListItem item = drpDiaBan.Items.FindByValue(value);
                if(item!=null) item.Selected = true;
            } 
            get {
                return drpDiaBan.SelectedValue;
            } 
        }
        public String TenDiaBan
        {
            get
            {
                String temp = "";
                if (drpDiaBan.SelectedItem != null) temp = drpDiaBan.SelectedItem.Text;
                return temp;
            }
        }
        public IList<DiaBan> DiaBanList
        {
            set
            {
                drpDiaBan.DataSource = value;
                drpDiaBan.DataBind();
            }
        }
        public String Email
        {
            set { txtEmail.Text = value; }
            get { return txtEmail.Text.Trim(); }
        }
        public String EmailPass
        {
            set { txtPassEmail.Text = value; }
            get { return txtPassEmail.Text.Trim(); }
        }
        public bool IsFinishUpdate
        {
            set { this.isFinishUpdate = value; }
            get { return this.isFinishUpdate; }
        }
        public String Msg
        {
            set { this.lblMsg.Text = value; }
        }
        public bool IsValid
        {
            get { throw new Exception("The method or operation is not implemented."); }
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            this.RaiseClientEvent();
            if (!this.IsPostBack)
            {
                Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
                this.presenter.LoadViewFromModel(config);
            }
        }

        protected void RaiseClientEvent()
        {
            Utility.RegisterTypeForAjax(typeof(CMS.Presenters.AppSettingsPresenter));
            //drpDiaBan.Attributes.Add("onclick", "return changeDiaBan(this.value);");
            btnOK.Attributes.Add("onclick", "return checkValidationForm();");
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            // Get configuration.
            Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
            this.presenter.UpdateConfigs(config);
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            
        }
    }
}